from fastapi import Request, HTTPException, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import  RequestValidationError, ResponseValidationError
from fastapi.responses import JSONResponse


async def http_error_handler(request: Request, exc: HTTPException) -> JSONResponse:
    return JSONResponse(
        status_code=exc.status_code,
        content=exc.detail
    )


async def request_validationerror_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
    return_data = {"msg": "request 参数校验失败", "detail": exc.errors() }
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content=jsonable_encoder(return_data)
    )



async def response_validationerror_handler(request: Request, exc: ResponseValidationError) -> JSONResponse:
    return_data = {"msg": "response 数据校验失败", "detail": exc.errors() }
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content=jsonable_encoder(return_data)
    )